package com.mc2.docapp.disabilityassessmenttool;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.mc2.docapp.disabilityassessmenttool.Amputation.Class_Amputation;
import com.mc2.docapp.disabilityassessmenttool.ConginitalDef.Class_Conginital_Deficiency_component;
import com.mc2.docapp.disabilityassessmenttool.HandComponent.C_Hand;
import com.mc2.docapp.disabilityassessmenttool.HandComponent.C_upLimb_add_wgt;
import com.mc2.docapp.disabilityassessmenttool.LowerLimb.C_LL_active_rom;
import com.mc2.docapp.disabilityassessmenttool.LowerLimb.C_LL_muscle_power;
import com.mc2.docapp.disabilityassessmenttool.LowerLimb.Class_lowerLimb_component;
import com.mc2.docapp.disabilityassessmenttool.LowerLimb.Lower_Limb_MobStab_ExtraPoints;
import com.mc2.docapp.disabilityassessmenttool.LowerLimb.Lower_Limb_Mobility_Component;
import com.mc2.docapp.disabilityassessmenttool.SpinalAffection.C_Scoliosis_Component;
import com.mc2.docapp.disabilityassessmenttool.SpinalAffection.C_cerebral_palsy;
import com.mc2.docapp.disabilityassessmenttool.SpinalAffection.C_dwafism;
import com.mc2.docapp.disabilityassessmenttool.SpinalAffection.C_kyphosis;
import com.mc2.docapp.disabilityassessmenttool.SpinalAffection.C_neurological_deficit;
import com.mc2.docapp.disabilityassessmenttool.SpinalAffection.C_spin_affn_misc_med_conditions;
import com.mc2.docapp.disabilityassessmenttool.SpinalAffection.C_traumetic_affection;
import com.mc2.docapp.disabilityassessmenttool.SpinalAffection.Class_spinal_affection;
import com.mc2.docapp.disabilityassessmenttool.UpperLimb.C_UL_muscle_power;
import com.mc2.docapp.disabilityassessmenttool.UpperLimb.C_active_rom;
import com.mc2.docapp.disabilityassessmenttool.UpperLimb.C_loss_in_10_co_ordinated_activities;
import com.mc2.docapp.disabilityassessmenttool.UpperLimb.Class_UpperLimb_component;
import com.mc2.docapp.disabilityassessmenttool.UpperLimb.Class_arm_component;
import com.mc2.docapp.disabilityassessmenttool.account.PatientDTO;
import com.mc2.docapp.disabilityassessmenttool.wrapper_computation.FinalComputationDTO;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Locale;

import static com.mc2.docapp.disabilityassessmenttool.utils.Statics.getEditTexts;
import static com.mc2.docapp.disabilityassessmenttool.utils.Statics.router;

public class NewPatient extends AppCompatActivity {

    public static FinalComputationDTO finalDto = new FinalComputationDTO(new Class_UpperLimb_component(new Class_arm_component(new C_active_rom(), new C_UL_muscle_power(), new C_loss_in_10_co_ordinated_activities(), 0, 0),
            new C_Hand(), new C_upLimb_add_wgt(),0, 0, 0, 0),
            new Class_lowerLimb_component(new C_LL_active_rom(), new C_LL_muscle_power(),0 , 0, new Lower_Limb_Mobility_Component(), 0, 0, new Lower_Limb_MobStab_ExtraPoints(), 0, 0),
            new Class_Amputation(), new C_dwafism(),
            new Class_Conginital_Deficiency_component(), new Class_spinal_affection(new C_traumetic_affection(), new C_Scoliosis_Component(), new C_kyphosis(), new C_spin_affn_misc_med_conditions(),
            new C_neurological_deficit()), new C_cerebral_palsy(), 0);


    Spinner sexSpinner;
    DatePicker dateOfAssesment;
    private String gender[];
    public static PatientDTO pateint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_patient);
//
        EditText insti_name = (EditText)findViewById(R.id.insti_name);
        EditText record_no = (EditText)findViewById(R.id.record_no);
        EditText dateass = (EditText)findViewById(R.id.dateass);
        EditText pat_name = (EditText)findViewById(R.id.pat_name);
        EditText phone = (EditText)findViewById(R.id.phone);
        EditText dob = (EditText)findViewById(R.id.dob);
        EditText diagnosis = (EditText)findViewById(R.id.diagnosis);
        EditText taken_by = (EditText)findViewById(R.id.taken_by);
        EditText doc = (EditText)findViewById(R.id.doc);
        EditText docmail = (EditText) findViewById(R.id.docmail);
        //
        sexSpinner = (Spinner) findViewById(R.id.sexSpinner);

        gender = new String[]{"male", "Female", "Other"};
        sexSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        ArrayAdapter<String> spin_adapter = new ArrayAdapter<String>(NewPatient.this, android.R.layout.simple_spinner_item, gender);
        sexSpinner.setAdapter(spin_adapter);

        Button clearAll = (Button) findViewById(R.id.clear);
        clearAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashSet<EditText> views = new HashSet<>();
                getEditTexts((ViewGroup) findViewById(R.id.activity_main), views);
                for (EditText h : views)
                    h.setText("");
            }
        });


        Button saveNnextButon = (Button) findViewById(R.id.saveAndNext);
        saveNnextButon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HashSet<EditText> views = new HashSet<>();
                getEditTexts((ViewGroup) findViewById(R.id.activity_main), views);
                boolean isEmpty = false;
                for (EditText h : views) {
                    if (h.getText().toString().equals("")) {
                        isEmpty = true;
                        break;
                    }
                }
                if (isEmpty) {
                    Toast.makeText(getApplicationContext(),"All fields are mandatory",Toast.LENGTH_SHORT).show();
                    return;
                }

                pateint = new PatientDTO(insti_name.getText().toString()
                        , record_no.getText().toString()
                        , dateass.getText().toString()
                        , pat_name.getText().toString()
                        , phone.getText().toString()
                        , dob.getText().toString()
                        , sexSpinner.getSelectedItem().toString()
                        , diagnosis.getText().toString()
                        , taken_by.getText().toString()
                        , doc.getText().toString()
                        , docmail.getText().toString()
                );
                Intent intent = new Intent(NewPatient.this, router(NewPatient.class)/*UpLimbAcRom.class*/);
                startActivity(intent);
            }
        });

        //dateOfAssesment = (DatePicker) findViewById(R.id.dateOfAssmnt);

        final Calendar myCalendar = Calendar.getInstance();

        final EditText edittext = (EditText) findViewById(R.id.dob);
        final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {
                // TODO Auto-generated method stub
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                String myFormat = "dd/MM/yy"; //In which you need put here
                SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

                edittext.setText(sdf.format(myCalendar.getTime()));
            }

        };

        edittext.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(NewPatient.this, date, myCalendar
                        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });




        ///////////////////
        final Calendar myCalendar2 = Calendar.getInstance();
        final EditText edittext2 = (EditText) findViewById(R.id.dateass);
        final DatePickerDialog.OnDateSetListener date2 = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {
                // TODO Auto-generated method stub
                myCalendar2.set(Calendar.YEAR, year);
                myCalendar2.set(Calendar.MONTH, monthOfYear);
                myCalendar2.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                String myFormat = "dd/MM/yy"; //In which you need put here
                SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

                edittext2.setText(sdf.format(myCalendar2.getTime()));
            }

        };

        edittext2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(NewPatient.this, date2, myCalendar2
                        .get(Calendar.YEAR), myCalendar2.get(Calendar.MONTH),
                        myCalendar2.get(Calendar.DAY_OF_MONTH)).show();
            }
        });
        ///////////////////
    }


}
